# -*- ruby -*-
require 'rake'
require 'fileutils'
directory "examples"

rsvg_bin="rsvg-convert"

def get_base(f)
  f.sub(File.dirname(__FILE__)+"/../examples/","").gsub("/","_").gsub(".rb","")
end
EXAMPLES=Dir.glob(File.dirname(__FILE__)+"/../examples/**/*.rb").map {|v|     [v,get_base(v)]
}.find_all{|v| !v[0].include?"_data"}

EXAMPLES_BASE=EXAMPLES.map {|v| v[1]}


desc "Build all html and svg files"
task :build_site do
  ruby "build_site.rb"
end


task :clean do
  Dir.glob(File.dirname(__FILE__)+"/examples/*.svg").each do |t|
    FileUtils.rm t
  end
  Dir.glob(File.dirname(__FILE__)+"/examples/*.html").each do |t|
    FileUtils.rm t
  end
  Dir.glob(File.dirname(__FILE__)+"/examples/*.png").each do |t|
    FileUtils.rm t
  end
  
end
task :default=>["examples", "index.html", "../lib/rubyvis.rb"]+EXAMPLES_BASE.map {|v| "examples/#{v}.html"}

file "index.html"=>["index.haml", :build_site]+EXAMPLES_BASE.map {|v| "examples/#{v}.png"}

EXAMPLES.each do |v|
  e=v[1]
  file "examples/#{e}.html"=>["examples.haml", "examples/#{e}.svg", :build_site, v[0]]
  file "examples/#{e}.svg"=>[v[0]] do |t|
    if !system "ruby #{v[0]} > #{t.name}"
      FileUtils.rm t.name
      exit
    end
  end
  file "examples/#{e}.png"=>["examples/#{e}.svg"] do |t|
    system   "#{rsvg_bin}  -w 200 -h 200 -f png -o #{t.name} examples/#{e}.svg "
  end
end

load 'upload_task.rb' if File.exists? "upload_task.rb"
